package com.lry.basic.algorithm.bit;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/8/16
 */
public class BitMap {
    public final static int MAX = 1024;

    public static boolean isDuplicate(int[] arr){
            int[] tmp = new int[MAX];
            for(int i=0;i<arr.length;i++){
                if(tmp[arr[i]]==0){
                    tmp[arr[i]] = 1;
                }else{
                    return true;
                }
            }
            return false;
    }
    public static boolean isDuplicateByBit(int []arr){
        int[] tmp = new int[MAX/32+1];
        for(int i=0;i<arr.length;i++){
            int index = arr[i]>>5 ;//数组下标
            int pos = arr[i]%32;//数组下标pos对应的数的第多少位
            if((tmp[index]&((1<<pos)))!=0){
                return true;
            }
            tmp[index] |= (1<<pos);//将数组下标index对应的数的第pos位 置为1
        }
        return false;
    }
    //探测num第pos位是不是1（pos从0开始算）
    public static boolean FD_ISSET(int pos,int num){
        return (num&((1<<pos)))!=0;
    }
    public static int FD_SET(int pos,int num){
        return num |= (1<<pos);
    }
    public static int FD_CLR(int pos,int num){
        return  num &= ~(1<<pos);
    }
    public static void main(String[] args) {
        System.out.println(isDuplicateByBit(new int[]{1,2,3,1003,1023}));
        System.out.println(FD_ISSET(0,3));
        System.out.println(FD_SET(2,3));
        System.out.println(FD_CLR(1,30));
    }
}
